package com.company.runner;


import com.company.exception.NoMatchException;
import com.company.graph.EdgeGraph;
import com.company.graph.NodeGraph;
import com.company.graph.NopowerNoDirectEdgeWithNodeGraph;
import com.company.graph.NopowerNoDirectNodeGraph;
import com.company.graphgetter.NodeGetterWithGraph;
import com.company.process.GraphBAProcess;
import com.company.statistician.Column;
import com.company.statistician.NodeDistributedColumnStatistician;
import com.company.statistician.Statistician;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;

public class NodeTriangleCountRunner {
    //指定数据集文件的路径
    private static String INFILEPATH = DataSet.InputPath.WIKI_VOTE;
    private static String OUTFILEPATH = DataSet.OutputPath.BASE;
    //指定theta
    private static int lumbda = 128;

    public static void main(String[] args) {
        try {
            //打开了数据集的文件流
            FileInputStream fileInputStream = new FileInputStream(INFILEPATH);
            //通过这个文件流指向的数据集 创建我们的图对象
            NodeGraph G = textNode(fileInputStream);
            //计算图中的三角形的个数 可以求得总的三角形个数 也可以求的每一个节点单独的三角形个数
            System.out.println("[Before Processed] Getting Info Start......");
            NodeGetterWithGraph getterWithGraph = new NodeGetterWithGraph(G);
            System.out.println(getterWithGraph.result());
            System.out.println(".......Getting Info End");
            //处理之前 获得统计数据
            ArrayList<Column> columns = addWithZeroCount(getterWithGraph);
            Column.showValues(columns, DataSet.pathoffset(OUTFILEPATH));
            //处理图
            System.out.println("时间统计:");
            System.out.println(new Date(System.currentTimeMillis()));
            GraphBAProcess graphBAProcess = new GraphBAProcess(lumbda);
            graphBAProcess.process(G, getterWithGraph);
            System.out.println(new Date(System.currentTimeMillis()));
            System.out.println("Again, Getting Info Start......");
            //重新计算个数
            System.out.println("[After Processed] Getting Info Start......");
            getterWithGraph = new NodeGetterWithGraph(G);
            System.out.println(getterWithGraph.result());
            System.out.println(".......Getting Info End");
            //开始统计
            columns = addWithZeroCount(getterWithGraph);
            Column.showValues(columns, DataSet.pathoffset(OUTFILEPATH));
//            System.out.println(getterWithGraph.result());
        } catch (IOException | NoMatchException e) {
            e.printStackTrace();
        }
    }

    private static ArrayList<Column> addWithZeroCount(NodeGetterWithGraph getterWithGraph) throws NoMatchException {
        Column column;
        ArrayList<Column> columns;
        Statistician statistician;
        statistician = new NodeDistributedColumnStatistician(getterWithGraph, 1);
        statistician.equipData(getterWithGraph);
        statistician.calcData();
        columns = new ArrayList<>();
        column = new Column(0, 1, getterWithGraph.zeroCount());
        columns.add(column);
        columns.addAll((ArrayList<Column>) statistician.outputData());
        return columns;
    }

    private static EdgeGraph csvEdge(FileInputStream fileInputStream) throws IOException {
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
        BufferedReader in = new BufferedReader(inputStreamReader);
        String str;
        EdgeGraph G = new NopowerNoDirectEdgeWithNodeGraph();
//        EdgeGraph G = new NopowerNoDirectEdgeGraph();
        while ((str = in.readLine()) != null) {
            String[] arr = str.split("\\,");
            int from = Integer.parseInt(arr[0]);
            int to = Integer.parseInt(arr[1]);
            G.addEdge(from, to, Double.parseDouble(arr[2]));
        }
        return G;
    }

    private static EdgeGraph textEdge(FileInputStream fileInputStream) throws IOException {
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
        BufferedReader in = new BufferedReader(inputStreamReader);
        String str;
        EdgeGraph G = new NopowerNoDirectEdgeWithNodeGraph();
//        EdgeGraph G = new NopowerNoDirectEdgeGraph();
        while ((str = in.readLine()) != null) {
            String[] arr = str.split("\\ ");
            int from = Integer.parseInt(arr[0]);
            int to = Integer.parseInt(arr[1]);
            G.addEdge(from, to);
        }
        return G;
    }

    private static NopowerNoDirectNodeGraph textNode(FileInputStream fileInputStream) throws IOException {
        InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
        BufferedReader in = new BufferedReader(inputStreamReader);
        String str;
        NopowerNoDirectNodeGraph G = new NopowerNoDirectNodeGraph();
        while ((str = in.readLine()) != null) {
            String[] arr = str.split("\\ ");
            int from = Integer.parseInt(arr[0]);
            int to = Integer.parseInt(arr[1]);
            G.addEdge(from, to);
        }
        return G;
    }
}
